using System;
using System.Security.Cryptography;
using System.Text;

namespace Framework.Library.Components.Security
{
	public class SecurityMethod
	{
		public static string RandomString(int length)
		{
			string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			int strlen = str.Length;
			Random rnd = new Random();
			string retVal = String.Empty;

			for (int i = 0; i < length; i++)
				retVal += str[rnd.Next(strlen)];

			return retVal;
		}

		public static string MD5Encrypt(string plainText)
		{
			byte[] data, output;
			UTF8Encoding encoder = new UTF8Encoding();
			MD5CryptoServiceProvider hasher = new MD5CryptoServiceProvider();

			data = encoder.GetBytes(plainText);
			output = hasher.ComputeHash(data);

			return BitConverter.ToString(output).Replace("-", "").ToLower();
		}

		public static string RandomPassword()
		{
			string retVal = String.Empty;
			Random rd = new Random(DateTime.Now.Millisecond);
			for (int i = 1; i < 10; i++)
			{
				retVal += rd.Next(0, 9);
			}
			return retVal;
		}
	}
}